/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mfd2.frames;

/**
 *
 * @author Mr.Sam
 */
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import fr.ove.openmath.mfd2.*;
import fr.ove.openmath.mfd2.events.*;
import fr.ove.openmath.jome.Jome;
import fr.ove.openmath.exceptions.*;

/**
* The frame for entering the elements of the request of type containing.
*
* @author © 1999 DIRAT Laurent
* @version 1.0  27/10/1999
*/
public final class RequestContaining extends Frame implements OpenMathResultSetable {
	Panel panelContaining;
	Panel panelContainingRequest;
	Panel panelChecboxes;
	Panel panelOperators;
	Label labelOperators;
	Panel panelCheckboxOperators;
	Checkbox checkboxPlus;
	Checkbox checkboxMult;
	Checkbox checkboxMinus;
	Checkbox checkboxDivide;
	Checkbox checkboxPower;
	Panel panelRelations;
	Label labelRelations;
	Panel panelCheckboxRelational;
	Checkbox checkboxEq;
	Checkbox checkboxNeq;
	Checkbox checkboxLt;
	Checkbox checkboxLeq;
	Checkbox checkboxGt;
	Checkbox checkboxGeq;
	Panel panelFunctions;
	Label labelFuntions;
	Panel panelCheckboxFunctions;
	Checkbox checkboxLog;
	Checkbox checkboxLn;
	Checkbox checkboxExp;
	Checkbox checkboxSin;
	Checkbox checkboxCos;
	Checkbox checkboxTan;
	Checkbox checkboxArcsin;
	Checkbox checkboxArccos;
	Checkbox checkboxArctan;
	Panel panelButtons;
	Button buttonSendRequest;
	ScrollPane panelResultRequest;
	
	Jome jomeResult;

	PanelMfd2 panelMfd2;
	
	String containing;
	
    // The listeners
    private Vector listeners = new Vector();

	public RequestContaining(Frame parent, PanelMfd2 thePanelMfd2) {
		super("Looking for Data...");
		panelMfd2 = thePanelMfd2;
		setLayout(new BorderLayout());
		setBackground(Color.lightGray);
		
		setSize(510,340);
				
		panelContaining = new Panel();
		panelContaining.setLayout(new FlowLayout());
		panelContaining.setFont(new Font("Dialog", Font.PLAIN, 16));
		add("West", panelContaining);
		
		panelContainingRequest = new Panel();
		panelContainingRequest.setLayout(new BorderLayout(0,2));
		panelContaining.add("West", panelContainingRequest);
		
		panelChecboxes = new Panel();
		panelChecboxes.setLayout(new BorderLayout(0,5));
		panelContainingRequest.add("West", panelChecboxes);
		
		panelOperators = new Panel();
		panelOperators.setLayout(new BorderLayout(0,0));
		panelChecboxes.add("North", panelOperators);
		labelOperators = new Label("Basic Operators");
		panelOperators.add("North", labelOperators);
		
		panelCheckboxOperators = new Panel();
		panelCheckboxOperators.setLayout(new GridLayout(2,3,0,0));
		panelCheckboxOperators.setFont(new Font("Dialog", Font.PLAIN, 14));
		panelOperators.add("Center", panelCheckboxOperators);
		checkboxPlus = new Checkbox("plus");
		panelCheckboxOperators.add(checkboxPlus);
		checkboxMult = new Checkbox("mult");
		panelCheckboxOperators.add(checkboxMult);
		checkboxMinus = new Checkbox("minus");
		panelCheckboxOperators.add(checkboxMinus);
		checkboxDivide = new Checkbox("divide");
		panelCheckboxOperators.add(checkboxDivide);
		checkboxPower = new Checkbox("power");
		panelCheckboxOperators.add(checkboxPower);
		
		panelRelations = new Panel();
		panelRelations.setLayout(new BorderLayout(0,0));
		panelChecboxes.add("Center", panelRelations);
		labelRelations = new Label("Relational Operators");
		panelRelations.add("North", labelRelations);
		
		panelCheckboxRelational = new Panel();
		panelCheckboxRelational.setLayout(new GridLayout(2,4,0,0));
		panelCheckboxRelational.setFont(new Font("Dialog", Font.PLAIN, 14));
		panelRelations.add("Center", panelCheckboxRelational);
		checkboxEq = new Checkbox("eq");
		panelCheckboxRelational.add(checkboxEq);
		checkboxNeq = new Checkbox("neq");
		panelCheckboxRelational.add(checkboxNeq);
		checkboxLt = new Checkbox("lt");
		panelCheckboxRelational.add(checkboxLt);
		checkboxLeq = new Checkbox("leq");
		panelCheckboxRelational.add(checkboxLeq);
		checkboxGt = new Checkbox("gt");
		panelCheckboxRelational.add(checkboxGt);
		checkboxGeq = new Checkbox("geq");
		panelCheckboxRelational.add(checkboxGeq);
		
		panelFunctions = new Panel();
		panelFunctions.setLayout(new BorderLayout(0,0));
		panelChecboxes.add("South", panelFunctions);
		labelFuntions = new Label("Transcendantal Functions");
		panelFunctions.add("North", labelFuntions);
		
		panelCheckboxFunctions = new Panel();
		panelCheckboxFunctions.setLayout(new GridLayout(3,3,0,0));
		panelCheckboxFunctions.setFont(new Font("Dialog", Font.PLAIN, 14));
		panelFunctions.add("Center", panelCheckboxFunctions);
		checkboxLog = new Checkbox("log");
		panelCheckboxFunctions.add(checkboxLog);
		checkboxLn = new Checkbox("ln");
		panelCheckboxFunctions.add(checkboxLn);
		checkboxExp = new Checkbox("exp");
		panelCheckboxFunctions.add(checkboxExp);
		checkboxSin = new Checkbox("sin");
		panelCheckboxFunctions.add(checkboxSin);
		checkboxCos = new Checkbox("cos");
		panelCheckboxFunctions.add(checkboxCos);
		checkboxTan = new Checkbox("tan");
		panelCheckboxFunctions.add(checkboxTan);
		checkboxArcsin = new Checkbox("arcsin");
		panelCheckboxFunctions.add(checkboxArcsin);
		checkboxArccos = new Checkbox("arccos");
		panelCheckboxFunctions.add(checkboxArccos);
		checkboxArctan = new Checkbox("arctan");
		panelCheckboxFunctions.add(checkboxArctan);
		
		panelButtons = new Panel();
		panelButtons.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
		panelButtons.setFont(new Font("Dialog", Font.PLAIN, 14));
		panelContainingRequest.add("South", panelButtons);
		
		buttonSendRequest = new Button();
		buttonSendRequest.setLabel("Send Request");
		panelButtons.add(buttonSendRequest);
		
		buttonSendRequest.addActionListener(
		    new ActionListener() {
        		public void actionPerformed(ActionEvent event) {
        		    boolean gotToSend = false;
        		    containing = "<OMOBJ>\n  <OMA>\n    <OMS cd=\"mfd2\" name=\"containing\"/>\n";
        		    
            		if (checkboxPlus.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"arith1\" name=\"plus\"/>\n";
            		}
            		
            		if (checkboxMult.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"arith2\" name=\"times\"/>\n";
            		}
            		
            		if (checkboxMinus.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"arith1\" name=\"minus\"/>\n";
            		}
            		
            		if (checkboxDivide.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"arith1\" name=\"divide\"/>\n";
            		}
            		
            		if (checkboxPower.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"arith1\" name=\"power\"/>\n";
            		}
            		
            		if (checkboxEq.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"relation1\" name=\"eq\"/>\n";
            		}
            		
            		if (checkboxNeq.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"relation1\" name=\"neq\"/>\n";
            		}
            		
            		if (checkboxLt.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"relation1\" name=\"lt\"/>\n";
            		}
            		
            		if (checkboxLeq.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"relation1\" name=\"leq\"/>\n";
            		}
            		
            		if (checkboxGt.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"relation1\" name=\"gt\"/>\n";
            		}
            		
            		if (checkboxGeq.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"relation1\" name=\"geq\"/>\n";
            		}
            		
            		if (checkboxLog.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"transc1\" name=\"log\"/>\n";
            		}
            		
            		if (checkboxLn.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"transc1\" name=\"ln\"/>\n";
            		}
            		
            		if (checkboxExp.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"transc1\" name=\"exp\"/>\n";
            		}
            		
            		if (checkboxSin.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"transc1\" name=\"sin\"/>\n";
            		}
            		
            		if (checkboxCos.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"transc1\" name=\"cos\"/>\n";
            		}
            		
            		if (checkboxTan.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"transc1\" name=\"tan\"/>\n";
            		}
            		
            		if (checkboxArcsin.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"transc1\" name=\"arcsin\"/>\n";
            		}
            		
            		if (checkboxArccos.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"transc1\" name=\"arccos\"/>\n";
            		}
            		
            		if (checkboxArctan.getState()) {
            		    gotToSend = true;
            		    containing += "    <OMS cd=\"transc1\" name=\"arctan\"/>\n";
            		}
            		

        		    if (gotToSend) {
        		        panelMfd2.setIsConstainingRequest(true);
        		        
        		        Selector selector = panelMfd2.getSelector();
                        selector.setOwner(RequestContaining.this);
        		        
            		    containing += "  </OMA>\n</OMOBJ>";
            		    
                	    RequestContainingEvent requestContainingEvent = new RequestContainingEvent(RequestContaining.this);
                	    requestContainingEvent.setAction(RequestContainingEvent.UPDATE_REQUEST, null);
                	    fireRequestContainingEvent(requestContainingEvent);
                	    
        		        panelMfd2.setObject(containing.getBytes());
        		    }
        		}
        	}
        );
		
		
		panelResultRequest = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);

		panelResultRequest.setBackground(new Color(16777215));
		add("Center", panelResultRequest);
		
		jomeResult = new Jome();
		jomeResult.setDrawBounds(false);
		
		jomeResult.setLayout(new FlowLayout(FlowLayout.LEFT,10,5));
		jomeResult.setFont(new Font("Times New Roman", Font.PLAIN, 14));
		panelResultRequest.add(jomeResult);

		// Le listener pour pouvoir fermer la fenêtre
		addWindowListener(
		    new WindowAdapter() {
        		public void windowClosing(WindowEvent event) {
                    dispose();
        		}
        	}
        );
	}
	
	public void setOpenMathResult(String result) throws ErrorMessageException {
	    try {
    	    jomeResult.setOpenMath(result);
    	    
    	    RequestContainingEvent requestContainingEvent = new RequestContainingEvent(this);
    	    requestContainingEvent.setAction(RequestContainingEvent.UPDATE_RESULT, null);
    	    fireRequestContainingEvent(requestContainingEvent);
    	}
    	catch (Exception e) {
    	    throw((ErrorMessageException) e);
    	}
	}
	
    public String getOpenMathRequest() {
        return containing;
    }
    
    public String getOpenMathResult() {
        return jomeResult.getOpenMath();
    }
    
    public Jome getJomeResult() {
        return jomeResult;
    }
    
    /**
    */
    public void addRequestContainingListener(RequestContainingListener requestContainingListener) {
        listeners.addElement(requestContainingListener);
    }

    /**
    */
    public void removeRequestContainingListener(RequestContainingListener requestContainingListener) {
        listeners.removeElement(requestContainingListener);
    }

    /**
    * Fires a RequestContainingEvent event to registered listeners.
    * @param requestContainingEvent event encapsulating relevant information.
    */
    public void fireRequestContainingEvent(RequestContainingEvent requestContainingEvent) {
        for (int i = 0; i < listeners.size(); i++)
          ((RequestContainingListener)listeners.elementAt(i)).consumeRequestContainingEvent(requestContainingEvent);
    }
}

